﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Tiscs.Practices.Web.Services.Client
{
    /// <summary>
    /// 服务Cookie管理器。单例模式。
    /// </summary>
    public sealed class ServiceCookieManager : INotifyPropertyChanged
    {
        private static ServiceCookieManager _current;

        /// <summary>
        /// 当前服务Cookie管理器实例。
        /// </summary>
        public static ServiceCookieManager Current
        {
            get
            {
                if (_current == null)
                {
                    _current = new ServiceCookieManager();
                }
                return _current;
            }
        }

        /// <summary>
        /// 私有构造方法。
        /// </summary>
        private ServiceCookieManager()
        {
        }

        private string _serviceCookie;

        /// <summary>
        /// 获取当前服务Cookie。
        /// </summary>
        public string ServiceCookie
        {
            get
            {
                return _serviceCookie;
            }
            set
            {
                string serviceCookie = _serviceCookie;
                _serviceCookie = value;
                if (!string.Equals(serviceCookie, value))
                {
                    if (CookieChanged != null)
                    {
                        CookieChanged(this, EventArgs.Empty);
                    }
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(ServiceCookie));
                    }
                }
            }
        }

        /// <summary>
        /// 继承自<see cref="INotifyPropertyChanged"/>接口的事件。
        /// 属性变化通知事件。
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Cookie变化事件。
        /// </summary>
        public event EventHandler CookieChanged;
    }
}
